package com.lhf.azkaban.springbatch.example.job.config;

import com.lhf.azkaban.springbatch.example.job.listener.MyJobListener;
import com.lhf.azkaban.springbatch.example.job.listener.MyStepListener;
import com.lhf.azkaban.springbatch.example.job.service.TaskletStepService;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.launch.support.RunIdIncrementer;
import org.springframework.batch.core.step.tasklet.MethodInvokingTaskletAdapter;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Author:95780
 * @Date: 15:36 2019/11/21
 * @Description:
 */
//@Configuration
public class JobListenerConfig extends BaseConfig {
    @Autowired
    MyJobListener myJobListener;
    @Autowired
    MyStepListener myStepListener;

    @Autowired
    TaskletStepService taskletStepService;


    @Bean//(name = "jobListenerJob")
    public Job taskLetJob() {
        return jobBuilderFactory.get("jobListenerJob")
                .incrementer(new RunIdIncrementer())
                .listener(myJobListener)
                .start(jobListenerStep())
                .build();
    }

    @Bean
    public Step jobListenerStep() {
        return stepBuilderFactory.get("jobListenerStep").listener(myStepListener)
                .tasklet(taskletExecOk())
                //.tasklet(taskletExecError())
                //.taskExecutor(new SyncTaskExecutor())
                //.throttleLimit(5)
                .build();
    }



    @Bean
    public Tasklet taskletExecOk(){
        MethodInvokingTaskletAdapter tasklet = new MethodInvokingTaskletAdapter();
        tasklet.setTargetObject(taskletStepService);
        tasklet.setTargetMethod("execOk");
        return tasklet;
    }

    @Bean
    public Tasklet taskletExecError(){
        MethodInvokingTaskletAdapter tasklet = new MethodInvokingTaskletAdapter();
        tasklet.setTargetObject(taskletStepService);
        tasklet.setTargetMethod("execError");
        return tasklet;
    }
}
